package com.bei_en.teacher.admin.controller.consultation;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.consultation.bean.ConsultationCollection;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationCollectionCondition;
import com.bei_en.teacher.admin.model.consultation.condition.NEWConsultationCollectionCondition;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationCollectionVo;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationVo;
import com.bei_en.teacher.admin.service.consultation.ConsultationCollectionService;
import com.bei_en.teacher.admin.service.consultation.ConsultationService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-3 11:54:27
 */
@Controller
@RequestMapping("/api/Consultation")
public class ConsultationCollectionAPPController extends BaseController {
	private static String bucketName = "jiaoshi";
	private static String img_endpoint = "oss-cn-hangzhou.aliyuncs.com";// 图片服务
	@Autowired
	ConsultationCollectionService consultationCollectionService;
	@Autowired
	ConsultationService consultationService;

	@RequestMapping("/consultationCollection")
	@ResponseBody
	@OAuthRequired
	public JsonResult consultationCollection(Model model,
			ConsultationCollectionCondition vo, BindingResult br,
			HttpSession session) {

		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			List<ConsultationCollectionVo> list = new ArrayList<ConsultationCollectionVo>();
			list = consultationCollectionService
					.findConsultationCollectionPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			ConsultationVo consultationVo = null;
			ConsultationCollectionVo consultationCollectionVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				consultationCollectionVo = list.get(i);
				consultationVo = consultationService
						.findById(consultationCollectionVo.getConsultationId());
				object.put("title", consultationVo.getTitle());
				object.put("Id", consultationVo.getId());
				object.put("create_time", consultationVo.getCreateTime()
						.getTime());
				object.put("look_num", consultationVo.getLookNum());
				object.put("thumbnail", "http://" + bucketName + "."
						+ img_endpoint + "/" +consultationVo.getThumbnail());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/consultationCollectionAdd")
	@ResponseBody
	@OAuthRequired
	public JsonResult consultationCollectionAdd(Model model,
			@Valid NEWConsultationCollectionCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			ConsultationCollectionVo consultationCollection = new ConsultationCollectionVo();
			consultationCollection
					.setCollectionPeople(vo.getCollectionPeople());
			consultationCollection.setConsultationId(vo.getConsultationId());
			consultationCollection.setUserId(vo.getUserId());
			ConsultationCollectionCondition collectionCondition=new ConsultationCollectionCondition();
			collectionCondition.setConsultationId(vo.getConsultationId());
			collectionCondition.setUserId(vo.getUserId());
			List<ConsultationCollectionVo> collectionVos=  consultationCollectionService.findConsultationCollectionPage(collectionCondition);
			if(!collectionVos.isEmpty()){
				ConsultationCollectionVo collectionVo=null;
				for (int i = 0; i < collectionVos.size(); i++) {
					collectionVo=collectionVos.get(i);
					consultationCollectionService.delete(collectionVo.getId());
				}
				return new JsonSuccessResult(session.getId(), "取消收藏成功");
			}
			consultationCollection.setCreateTime(new Date());
			consultationCollectionService.insert(consultationCollection);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/consultationCollectionEdit")
	public JsonResult consultationCollectionEdit(Model model,
			ConsultationCollectionCondition vo, BindingResult br,
			HttpSession session) {

		ConsultationCollectionVo consultationCollection = new ConsultationCollectionVo();

		consultationCollectionService.updateSelective(consultationCollection);

		return new JsonSuccessResult(session.getId(), new Array());
	}

}
